/**
 * 获取订单详情接口
 */

//引入Q模块
let Q = require('q');
//引入公共函数
let util = require('../../util');

//getOrderDetail接口入口文件
let index = function(getData, postData, config, redis, db) {
	let deferred = Q.defer();
	if(postData.orderId) {
		db.select('storeOrder', {orderId: postData.orderId}).then(function(orderData) {
			if(orderData.length == 0) {
				//订单详情接口未找到对应订单
				deferred.reject('-0180');
			}
			else {
				orderData = orderData[0];
				let orData = [];
				for(let goodsIndex in orderData.goodsList) {
					if(orderData.goodsList[goodsIndex].foodId) {
						orData.push({foodId: orderData.goodsList[goodsIndex].foodId});
					}
				}
				db.select('foodMenu', {$or: orData}).then(function(foodList) {
					let goodsList = orderData.goodsList;
					orderData.goodsList = null;
					for(let goodsIndex in goodsList) {
						for(let foodIndex in foodList) {
							if(goodsList[goodsIndex].foodId == foodList[foodIndex].foodId) {
								goodsList[goodsIndex].foodPhoto = foodList[foodIndex].foodPhoto;
							}
						}
					}
					deferred.resolve({
						orderData: orderData,
						goodsList: goodsList
					});
				}, function() {
					//获取菜单列表数据失败
					deferred.reject('-0063');
				});
			}
		}, function() {
			//订单详情接口查询对应订单号的订单失败
			deferred.reject('-0179');
		});
	}
	else {
		//获取指定天的订单列表接口参数不足
		deferred.reject('-0178');
	}
	return deferred.promise;
}

//暴露接口
module.exports = {
	index: index
}